package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.dto.Goods;
import com.qf.mapper.GoodsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsService extends ServiceImpl<GoodsMapper , Goods> {


    @Transactional
    public void up(Long goodsId) {

       // QueryWrapper<Goods> wrapper = new QueryWrapper<>();  查询
        UpdateWrapper<Goods> wrapper = new UpdateWrapper<>(); // 修改   这里用到了泛型  更加好维护
        wrapper.lambda().eq(Goods::getId , goodsId).set(Goods::getState , Goods.State.UP.getCode());
        this.update(wrapper);
    }

    @Transactional
    public void down(Long goodsId) {

        UpdateWrapper<Goods> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Goods::getId , goodsId).set(Goods::getState , Goods.State.DOWN.getCode());
        this.update(wrapper);
    }
    //通过登入者来显示商品
    @Transactional(readOnly = true)
    public List<Goods> getByMerchantUserId(Long merchantUSerId) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Goods::getMerchantUserId , merchantUSerId).orderByDesc(Goods::getCreate_time);

        return this.list(wrapper);
    }
}
